<template>
  <div>
    <div class="myumeremove" v-show="Industry&&positionshow">
      <div>
        <div class="checkremove">
          <van-row>
            <van-col span="12">
              <van-checkbox v-model="checked" :disabled="resumeid!==''?false:true" @click="resumeUpdateExpress">快速投递
              </van-checkbox>
            </van-col>
            <van-col span="12">
              <van-checkbox class="rightremo" v-model="checkedgk" :disabled="resumeid!==''?false:true"
                @click="resumeUpdatePublicity">公开简历</van-checkbox>
            </van-col>
          </van-row>
        </div>
        <div class="formrwsum">
          <van-form @submit="onSubmit" label-align="left" style="position: relative;">
            <div class="nvebarground"></div>
            <van-collapse v-model="activeNames" :accordion="true" :border="false">
              <van-collapse-item name="1">
                <template #title>
                  <span style="font-weight: bold;">
                    个人信息/求职信息
                  </span>
                </template>

                <van-field v-model="formdata.title" name="title" label="简历名称" placeholder="请填写简历名称" :rules="[{ required: true, message: ' ' },
                    { pattern: /^[^\s]*$/, message: ' '}]" />
                <van-field v-model="formdata.name" name="username" label="姓名" placeholder="请填写姓名" :rules="[{ required: true, message: ' ' },
                    { pattern: /^[^\s]*$/, message: ' '}]" />
                <van-field v-model="formdata.idCard" :maxlength="18" @blur="idcardBlur" @input="idcardBlur"
                  label-width="1.6rem" name="idcard" label="身份证号" placeholder="请填写身份证号"
                  :rules="[{ required: true, message: ' ' },
                    { pattern: /^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/, message: ' '}]" />
                <van-field readonly clickable name="xb" v-model="inputForm.xb" label="性别" />

                <van-field readonly clickable name="csdatetim" v-model="birthday" label="出生日期" />

                <van-field readonly clickable label-width="1.6rem" name="placeOfbirth" v-model="placeOfbirth"
                  label="籍贯" />

                <van-field readonly clickable name="jzd" :value="inputForm.jzd" label="居住地" placeholder="请选择居住地"
                  @click="showPickerjzd = true" :rules="[{ required: true, message: ' ' }]" />
                <van-popup v-model="showPickerjzd" position="bottom">
                  <van-picker show-toolbar value-key="name" :item-height="45" :columns="columnsqwdd"
                    @confirm="onConfirmjzd" @cancel="showPickerjzd = false" />
                </van-popup>
                <!-- feat：人员类别 -->
                <van-field readonly clickable :value="inputForm.peopleType" label="人员类别" placeholder="请选择人员类别"
                  @click="showpeopleType = true" />
                <van-popup v-model="showpeopleType" position="bottom">
                  <van-picker show-toolbar value-key="name" :columns="peopleType" @confirm="onpeopleType"
                    @cancel="showpeopleType = false" :item-height="45" />
                </van-popup>
                <!-- feat：人员类别 -->

                <van-field readonly clickable name="showTimecjgz" :value="workTime" label="参加工作时间"
                  placeholder="请选择参加工作时间" @click="showTimecjgz = true" />
                <van-popup v-model="showTimecjgz" position="bottom">
                  <van-datetime-picker type="date" v-model="currentDatework" :item-height="45" :maxDate="maxDate"
                    :minDate="minDatework" @confirm="onConfirmtimecjgz" @cancel="showTimecjgz = false" />
                </van-popup>

                <van-field readonly clickable name="gzxz" :value="inputForm.gzxz" label="工作性质" placeholder="请选择工作性质"
                  @click="showPickergzxz = true" :rules="[{ required: true, message: ' ' }]" />
                <van-popup v-model="showPickergzxz" position="bottom">
                  <van-picker show-toolbar value-key="name" :columns="columnswork" :item-height="45"
                    @confirm="onConfirmwork" @cancel="showPickergzxz = false" />
                </van-popup>

                <van-field class="qzztresume" readonly clickable name="qzzt" :value="inputForm.qzzt" label="求职状态"
                  label-width="1.6rem" placeholder="请选择求职状态" @click="showPickerqzzt = true"
                  :rules="[{ required: true, message: ' ' }]" />
                <van-popup v-model="showPickerqzzt" position="bottom">
                  <van-picker show-toolbar value-key="name" :visible-item-count="5" :item-height="45"
                    :columns="columnsqzzt" @confirm="onConfirmqzzt" @cancel="showPickerqzzt = false" />
                </van-popup>

                <van-field readonly clickable name="qzzt" :value="inputForm.qwdd" label="期望地点" placeholder="请选择期望地点"
                  @click="showPickerqwdd = true" :rules="[{ required: true, message: ' ' }]" />
                <van-popup v-model="showPickerqwdd" position="bottom">
                  <van-picker show-toolbar value-key="name" :columns="columnsqwdd" :item-height="45"
                    @confirm="onConfirmgqwdd" @cancel="showPickerqwdd = false" />
                </van-popup>

                <van-field readonly class="qzztresume" name="qwhy" :value="inputForm.qwhy" label="期望行业"
                  label-width="1.6rem" @click="industryca" placeholder="请选择期望行业"
                  :rules="[{ required: true, message: ' ' }]" />

                <van-field readonly class="qzztresume" name="qwzw" :value="inputForm.qwzw" label="期望职位"
                  label-width="1.6rem" @click="positionon" placeholder="请选择期望职位"
                  :rules="[{ required: true, message: ' ' }]" />
                <van-field v-model="formdata.hopeWage" type="digit" label="期望月薪(月/元)" placeholder="请选择期望月薪"
                  :maxlength="7" :rules="[{ required: true, message: ' ' },
                   {validator,message: '税前月薪不能超过5000000'}
                  ]" />
              </van-collapse-item>
            </van-collapse>
            <van-button round block type="info" :disabled="buttondisabled" class="sunminbtn" native-type="submit">提交
            </van-button>
          </van-form>
        </div>

        <div class="nvebarground"></div>
        <educational ref="Educational"></educational>
        <div class="nvebarground"></div>
        <work-experience ref="WorkExperience"></work-experience>
      </div>
    </div>
    <industry-category ref="IndustryCategory" type="digit" v-on:industryca="industryca" v-show="!Industry">
    </industry-category>
    <position-desired ref="PositionDesired" type="digit" v-on:positionon="positionon" v-show="!positionshow">
    </position-desired>
    <div style="height:1.4rem"></div>
  </div>
</template>
<script>
import DICTIONARY from '@/api/Dictionary'
import RESUMEMANAGEMENT from '@/api/ResumeManagement'
import Educational from '@/components/resumeManagement/educational'
import WorkExperience from '@/components/resumeManagement/workExperience'
import IndustryCategory from '@/components/resumeManagement/industryCategory'
import PositionDesired from '@/components/resumeManagement/positionDesired'
import { getBirth, getSex } from '@/assets/js/iDcard'

export default {
  components: {
    Educational,
    WorkExperience,
    IndustryCategory,
    PositionDesired
  },
  data () {
    return {
      result: [],
      buttondisabled: false,
      activeNames: '1',
      Industry: true,
      positionshow: true,
      checked: false,
      checkedgk: false,
      showPicker: false,
      showTime: false,
      showpeopleType: false,
      maxDate: new Date(),
      minDate: new Date(1970, 0, 1),
      minDatework: new Date(1970, 0, 1),
      bridtime: [],
      currentDate: new Date(),
      currentDatework: new Date(),
      showTimecjgz: false,
      showPickergzxz: false, // 工作性质
      showPickerqzzt: false, // 求职状态
      showPickerqwdd: false, // 期望地点状态
      showPickerjzd: false, // 居住地
      columns: [
        { code: 1, name: '男' },
        { code: 2, name: '女' }
      ], // 性别存储
      columnswork: [], // 工作性质存储
      columnsqzzt: [], // 求职状态存储
      columnsqwdd: [], // 期望地点存储
      // 人员类别
      peopleType: [],
      formdata: {
        title: '', // 简历名称
        name: '', // 姓名
        sex: 1, // 性别
        phone: '', // 手机号
        mail: '', // 邮箱
        hopeWage: '', // 期望月薪
        currentStatus: 1, // 求职状态
        workMode: '', // 工作性质
        workPlace: [], // 期望地点
        hopeTrade: [], // 期望行业
        hopeJob: [], // 期望职位
        idCard: '', // 身份证号
        // 人员类别
        peopleType: null,

        currentResidence: '', // 居住地
        domicilePlace: [] // 籍贯
      },
      placeOfdomicile: [], // 户籍地数据存储
      birthday: '', // 出生日期
      workTime: '', // 参加工作时间
      placeOfbirth: '', // 籍贯
      inputForm: {
        xb: '',
        gzxz: '', // 工作性质
        qzzt: '', // 求职状态
        qwdd: '', // 期望地点
        qwhy: '', // 期望行业
        qwzw: '', // 期望职位
        jzd: '',
        peopleType: '' // 默认传递null
      },
      resumeid: ''
    }
  },
  computed: {},
  methods: {
    // 获取子组件传过来的数据（期望行业）
    industryca (num, array) {
      let that = this
      // 判断显示隐藏
      this.Industry = !this.Industry
      console.log(array)
      let arr = [] // 临时保存子组件传过来的数据
      // 是接收数据还是传递数据，1表示接收数据
      if (num === 1) {
        this.formdata.hopeTrade = []
        for (let i = 0; i < array.length; i++) {
          this.formdata.hopeTrade.push(array[i].code)
          arr.push(array[i].text)
        } // 简历编辑给子组件传递数据
      } else if (this.inputForm.qwhy !== '') {
        console.log(this.inputForm.qwhy)
        // 向子组件传递参数
        this.$refs.IndustryCategory.substateindu(
          that.inputForm.qwhy,
          that.formdata.hopeTrade
        )
      }
      if (num === 2 && this.resumeid !== '') {
        this.selectEdit()
      }
      this.inputForm.qwhy = arr.join(',')
      console.log(this.inputForm.qwhe)
      this.$forceUpdate()
      console.log(array)
    },
    // 获取子组件传过来的数据（期望职业）
    positionon (num, array) {
      // 判断显示隐藏
      this.positionshow = !this.positionshow
      let that = this
      console.log(1)
      let arr = [] // 临时保存子组件传过来的数据
      if (num === 1) {
        that.formdata.hopeJob = []
        // 是接收数据还是传递数据，1表示接收数据
        for (let i = 0; i < array.length; i++) {
          that.formdata.hopeJob.push(array[i].code)
          arr.push(array[i].text)
        } // 简历编辑给子组件传递数据
      } else if (this.inputForm.qwzw !== '') {
        // 向子组件传递参数
        that.$refs.PositionDesired.substateindu(
          that.inputForm.qwzw,
          that.formdata.hopeJob
        )
      }
      if (num === 2 && this.resumeid !== '') {
        this.selectEdit()
      }
      that.inputForm.qwzw = arr.join(',')
      console.log(this.inputForm.qwzw, 's')
      this.$forceUpdate()
      console.log(array)
    },
    // 验证税前薪资
    validator (val) {
      return val <= 5000000
    },
    // 根据身份证号获取出生日期和性别
    idcardBlur () {
      console.log(getBirth(this.formdata.idcard))
      if (
        /^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/.test(
          this.formdata.idCard
        )
      ) {
        // 获取性别
        this.inputForm.xb = getSex(this.formdata.idCard)
        if (this.inputForm.xb === '男') {
          this.formdata.sex = 1
        } else {
          this.formdata.sex = 2
        }
        // 获取出生日期
        this.birthday = getBirth(this.formdata.idCard)
        let timedata = new Date(this.birthday).getTime()
        timedata = new Date(timedata)
        this.minDatework = new Date(
          timedata.getFullYear(),
          timedata.getMonth(),
          timedata.getDate()
        )

        // 获取籍贯
        let placeOfbirth = this.formdata.idCard.substring(0, 6)
        this.formdata.domicilePlace = []
        // 循环省
        this.placeOfdomicile.map(item => {
          if (item.childVo) {
            // 循环市
            item.childVo.map((val, i) => {
              // 循环区县
              item.childVo[i].childVo.map((name, j) => {
                // 如果和身份证前六位相等，就获取
                if (item.childVo[i].childVo[j].code === placeOfbirth) {
                  this.placeOfbirth =
                    item.name +
                    '/' +
                    item.childVo[i].name +
                    '/' +
                    item.childVo[i].childVo[j].name
                  this.formdata.domicilePlace.push(
                    item.code,
                    item.childVo[i].code,
                    item.childVo[i].childVo[j].code
                  )
                }
              })
            })
          }
        })
      }
    },
    // 性别选择
    onConfirm (value) {
      this.inputForm.xb = value.name
      this.formdata.sex = value.code
      console.log(value)
      this.showPicker = false
    },

    // 参加工作时间选择
    onConfirmtimecjgz (date) {
      console.log(date)
      let year = date.getFullYear()
      let month = date.getMonth() + 1
      let day = date.getDate()
      if (month >= 1 && month <= 9) {
        month = `0${month}`
      }
      if (day >= 1 && day <= 9) {
        day = `0${day}`
      }

      this.workTime = `${year}-${month}-${day}`
      this.showTimecjgz = false
    },
    // 职位字典获取
    adminDict () {
      let that = this
      let params = 'area,current_status,work_mode'
      DICTIONARY.adminDict(params).then(res => {
        if (res.data.code === 200) {
          that.columnswork = res.data.data.work_mode
          that.columnsqzzt = res.data.data.current_status
          that.columnsqwdd = res.data.data.area
        }
      })
      DICTIONARY.people_type().then(res => {
        console.log(res, 'res')
        if (res.data.success) {
          this.peopleType = res.data.data.people_type
        } else {
          console.log(res.data.msg, '失败')
        }
      })

    },
    // 居住地选择
    onConfirmjzd (value) {
      this.inputForm.jzd = value.name
      console.log(value)
      this.formdata.currentResidence = value.val
      this.showPickerjzd = false
    },
    // 工作性质选择
    onConfirmwork (value) {
      this.inputForm.gzxz = value.name
      console.log(value)
      this.formdata.workMode = value.val
      this.showPickergzxz = false
    },
    // 求职状态
    onConfirmqzzt (value) {
      this.inputForm.qzzt = value.name
      this.formdata.currentStatus = value.val
      this.showPickerqzzt = false
    },
    // 期望地点
    onConfirmgqwdd (value) {
      this.inputForm.qwdd = value.name

      this.formdata.workPlace[0] = value.val
      console.log(this.formdata.workPlace)
      this.showPickerqwdd = false
    },
    // 是否公开简历
    resumeUpdatePublicity () {
      let params = {}
      if (this.resumeid === '') {
        return false
      }
      if (this.checkedgk) {
        params.publicity = 1
      } else {
        params.publicity = 2
      }
      params.id = this.resumeid
      RESUMEMANAGEMENT.resumeUpdatePublicity(params).then(res => {
        console.log(res.data)
        if (res.data.code === 200) {
        }
      })
    },
    // 是否快速投递
    resumeUpdateExpress () {
      let params = {}
      if (this.resumeid === '') {
        return false
      }
      params.express = this.checked
      params.id = this.resumeid
      RESUMEMANAGEMENT.resumeUpdateExpress(params).then(res => {
        console.log(res.data)
        if (res.data.code === 200) {
        }
      })
    },
    // 获取全国省市区
    prFindtree () {
      DICTIONARY.prFindtree().then(res => {
        console.log(res.data)
        if (res.data.code === 200) {
          this.placeOfdomicile = res.data.data
        }
      })
    },
    // 简历新增
    onSubmit () {
      let that = this
      that.buttondisabled = true
      let params = this.formdata
      params.workPlace = JSON.stringify(this.formdata.workPlace)
      params.birthday = this.birthday + ' 00:00:00' //出生日期
      params.workTime = this.workTime + ' 00:00:00' //工作时间
      params.hopeWage = Number(that.formdata.hopeWage)//薪资
      params.id = this.resumeid
      params.idCard = this.formdata.idCard//身份证号
      params.domicilePlace = JSON.stringify(this.formdata.domicilePlace)//籍贯
      params.hopeJob = JSON.stringify(this.formdata.hopeJob)//期望职位
      params.hopeTrade = JSON.stringify(this.formdata.hopeTrade)//期望行业

      console.log(this.formdata)

      RESUMEMANAGEMENT.resumeInsertResumeByPhone(params).then(res => {
        if (res.data.code === 200) {
          if (res.data.data !== '') {
            that.resumeid = res.data.data
          }

          that.$refs.Educational.resumeidjy(that.resumeid)
          that.$refs.WorkExperience.resumeidjy(that.resumeid)
          // that.selectEdit()
          that.$toast('提交成功')
        } else {
          that.$notify(res.data.msg)
        }
        that.formdata.workPlace = JSON.parse(this.formdata.workPlace)
        that.formdata.hopeJob = JSON.parse(this.formdata.hopeJob)
        that.formdata.hopeTrade = JSON.parse(this.formdata.hopeTrade)
        that.formdata.domicilePlace = JSON.parse(this.formdata.domicilePlace)
        that.buttondisabled = false
      })

      this.$forceUpdate()
    },

    // 简历详情
    selectEdit () {
      let that = this
      console.log(111)
      RESUMEMANAGEMENT.selectEditResume(this.resumeid).then(res => {
        if (res.data.code === 200) {
          // that.formdata = res.data.data;
          that.inputForm.gzxz = res.data.data.workModeText
          that.inputForm.qzzt = res.data.data.currentStatusText
          if (!res.data.data.workPlace || res.data.data.workPlace === 'null') {
            that.formdata.workPlace = []
          } else {
            that.formdata.workPlace = JSON.parse(res.data.data.workPlace)
          }
          if (res.data.data.hopeWage === 'null') {
            that.formdata.hopeWage = ''
          }

          that.formdata.name = res.data.data.name//姓名
          that.formdata.sex = res.data.data.sex//性别id
          that.formdata.phone = res.data.data.phone//手机号
          that.formdata.mail = res.data.data.mail//邮箱
          that.formdata.currentStatus = res.data.data.currentStatus
          that.formdata.workMode = res.data.data.workMode
          that.formdata.hopeJob = JSON.parse(res.data.data.hopeJob)//职位id
          that.inputForm.qwzw = res.data.data.hopeJobText//职位名称
          that.inputForm.qwhy = res.data.data.hopeTradeText//行业名称
          that.formdata.domicilePlace = JSON.parse(res.data.data.domicilePlace)//籍贯id
          that.placeOfbirth = res.data.data.domicilePlaceText//籍贯名称
          that.formdata.hopeTrade = JSON.parse(res.data.data.hopeTrade)//行业id
          that.formdata.hopeWage = res.data.data.hopeWage
          that.inputForm.qwdd = res.data.data.workPlaceText//期望地点名称
          that.inputForm.xb = res.data.data.sexText//性别名称
          that.formdata.title = res.data.data.title//简历名称
          that.formdata.currentResidence = res.data.data.currentResidence
          that.inputForm.jzd = res.data.data.currentResidenceText//居住地
          that.inputForm.peopleType = res.data.data.peopleTypeText//人员类别
          that.formdata.idCard = res.data.data.idCard//身份证号
          that.checked = res.data.data.express
          if (res.data.data.publicity === 1) {
            that.checkedgk = true
          } else {
            that.checkedgk = false
          }
          that.$nextTick(() => {
            that.$refs.Educational.resumeidjy(
              that.resumeid,
              res.data.data.eduInfoOutVos
            )
            that.$refs.WorkExperience.resumeidjy(
              that.resumeid,
              res.data.data.workInfoOutVos
            )
          })
          console.log(res.data.data.birthday.split('T'))
          // res.data.data.birthday.split("T")
          that.birthday = res.data.data.birthday.split('T')[0]
          that.workTime = /\d{4}-\d{1,2}-\d{1,2}/g
            .exec(res.data.data.modelWorkTime)
            .toString()
        }
      })
    },
    // 选中数据
    onpeopleType (value) {
      this.inputForm.peopleType = value.name
      this.formdata.peopleType = value.val
      this.showpeopleType = false
    }

  },
  created () {
    let timedata = new Date(new Date().getTime() - 30758400000 * 18)
    this.bridtime = new Date(
      timedata.getFullYear(),
      timedata.getMonth(),
      timedata.getDate()
    )
    console.log(this.bridtime, 'time')
    console.log(1111)
    if (this.$route.query.resumeid) {
      this.resumeid = this.$route.query.resumeid

      this.selectEdit()
    }
    this.adminDict()
    this.prFindtree()
  }
}
</script>
<style scoped></style>
